﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using Stormy;
using System.Windows;
namespace GrooveSharp
{
    public static class DbUtil
    {
        public static string AsString(this IDataReader reader, string fieldName)
        {
            try
            {
                int index = reader.GetOrdinal(fieldName);
                if (!reader.IsDBNull(index))
                {
                    return reader[index].ToString();
                }
            }
            catch { }
            return null;
        }

        public static int? AsInteger(this IDataReader reader, string fieldName)
        {
            try
            {
                int index = reader.GetOrdinal(fieldName);
                if (!reader.IsDBNull(index))
                {
                    return Int32.Parse(reader[index].ToString());
                }
            }
            catch { }
            return null;
        }

        public static int AsInteger(this IDataReader reader, string fieldName, int defaultValue)
        {
            int? result = AsInteger(reader, fieldName);
            return result.HasValue ? result.Value : defaultValue;
        }

        public static Int64? AsLong(this IDataReader reader, string fieldName)
        {
            try
            {
                int index = reader.GetOrdinal(fieldName);
                if (!reader.IsDBNull(index))
                {
                    return Int64.Parse(reader[index].ToString());
                }
            }
            catch { }
            return null;
        }


        public static bool? AsBool(this IDataReader reader, string fieldName)
        {
            try
            {
                int index = reader.GetOrdinal(fieldName);
                if (!reader.IsDBNull(index))
                {
                    return Int16.Parse(reader[index].ToString()) != 0;
                }
            }
            catch { }
            return null;
        }

        public static bool AsBool(this IDataReader reader, string fieldName, bool defaultValue)
        {
            bool? result = AsBool(reader, fieldName);
            return result.HasValue && result.Value;
        }

        public static DateTime? AsDate(this IDataReader reader, string fieldName)
        {
            try
            {
                int index = reader.GetOrdinal(fieldName);
                if (!reader.IsDBNull(index))
                {
                    return DateTime.Parse(reader[index].ToString());
                }
            }
            catch { }
            return null;
        }

        public static TimeSpan? AsTimeSpan(this IDataReader reader, string fieldName)
        {
            try
            {
                int index = reader.GetOrdinal(fieldName);
                if (!reader.IsDBNull(index))
                {
                    return TimeSpan.Parse(reader[index].ToString());
                }
            }
            catch { }
            return null;
        }

        // Should be moved
        public delegate int DelegateGetId<T>(T input);
        public static int NewId<T>(IEnumerable<T> list, DelegateGetId<T> getId)
        {
            // Create a new item without ID yet (make it negative)
            int? newId = (from item in list select getId(item) ).Min();
            if (!newId.HasValue || newId.Value >= 0)
            {
                newId = -1;
            }
            else
            {
                newId--;
            }
            return newId.Value;
        }

    }
}
